<?php
require_once('conn.php');
$art = [];
$commont = [];
$repArr = [];
$CRArr = [];
//通过id查询文章
function byIdSelectArt($id)
{
  $sql = "select a.*,u.avatar,u.uName,(select count(*) from tbl_article_like where article_id = a.article_id) as like_count from  tbl_article  as a join tbl_user as u on a.uid=u.uid where a.article_id=$id;";
  $GLOBALS['art'] = exec_select($sql)[0];
  $sqlR = "select * from tbl_reply where article_id =$id;";
  $GLOBALS['repArr'] = exec_select($sqlR);
  // var_dump($GLOBALS['repArr']);
}
function byartSelectCom($id)
{
  $sqlC = "select c.*,u.avatar,u.uName ,(select count(*) from tbl_comment_like where comment_id = c.comment_id) as like_count from tbl_comment as c join tbl_article as a on c.article_id=a.article_id join tbl_user as u on u.uid=c.uid where a.article_id=$id order by c.created_time desc;";
  $GLOBALS['commont'] = exec_select($sqlC);
  // var_dump($GLOBALS['commont']);
  $reply = [];
  if (count($GLOBALS['commont']) > 1) {
    foreach ($GLOBALS['commont'] as $val) {
      $sqlR = "select r.*,u.uName,u.uid,u.avatar ,(select count(*) from tbl_reply_like where reply_id = r.reply_id) as like_count from tbl_reply as r join tbl_comment as c on c.comment_id=r.comment_id join tbl_user as u on u.uid= r.uid where c.comment_id=$val[comment_id];";
      $reply = exec_select($sqlR);
      $val += ['rep' => $reply];
      $GLOBALS['CRArr'][] = $val;
    }
  } else {
    if (count($GLOBALS['commont']) == 0) {
      $GLOBALS['CRArr'] = [];
    } else {
      $id = $GLOBALS['commont'][0]['comment_id'];
      $sqlR = "select r.*,u.uName,u.uid,u.avatar ,(select count(*) from tbl_reply_like where reply_id = r.reply_id) as like_count from tbl_reply as r join tbl_comment as c on c.comment_id=r.comment_id join tbl_user as u on u.uid= r.uid where c.comment_id=$id;";
      $GLOBALS['commont'][0] += ['rep' => exec_select($sqlR)];
      $GLOBALS['CRArr'] = $GLOBALS['commont'];
    }
  }
}

//查看用户是否点赞
function isUserLike($uid, $aid)
{
  $sql = "select * from tbl_article_like where uid=$uid and article_id=$aid;";
  if (count(exec_select($sql)) > 0) {
    return 'yes';
  } else {
    return 'no';
  }

}
?>